// ---------------------------------------------------------------------
//
//	Copyright 2003 e Professional Co.,Ltd.  All Rights Reserved.
//
//	File : checkValidData.js
//
//	Description : The checkValidData javascript  provides an easy way
//				to validate data in many type etc. number, date, email and 
//				compare value between two data input. It will send alert message
//				to user if data not valid for prevent error on web applications. 
//				It provides a variety of functions to customize the data, that 
//				will suitable for each type of data.
//
//-------------------------------------------------------------------- 

//------------------------------------------------------------------------
//
//	Function : isNull
//
//	Synopsis : This function is use for check null of data in object that send to
//				 this function. If data is null, it will send alert message to user. 
//
//	Arguments : This function receive 4 arguments (2 required) 
//			*	obj - Object that will check null value. 
//			*	fieldName - String that is name of object and it will  use for alert message. 
//				title - String that show in title bar of alert message dialog. 
//				okButtonLabel - String that show in ok button of alert message dialog. 
//
//	Returns : Boolean that (not)accept data that user key in
//
//	Notes : none
//
//------------------------------------------------------------------------
function isNull (obj, fieldName, title, okButtonLabel) {
	// Set default value for run function
	defaultTitle = "Data not complete"; // Default title of alert dialog
	defaultButtonLabel = "OK"; // Default title of alert dialog
	var returnValue = false; // Variable for return value

	// Check value of arguments and assign appropriate value
	title = (title == undefined || title == "") ? defaultTitle : title ;
	okButtonLabel = (okButtonLabel == undefined || okButtonLabel == "") ? defaultButtonLabel : okButtonLabel ;
	alertMessage = "Please enter : <B>"+fieldName+"</B>";

	// Check null value
		for(i = 0 ; i < obj.value.length ; i++) 
		{
			if(obj.value.substr(i, 1) != " ")
			{
				returnValue = true;
				break;
			}
		}

	// Show alert if check value is null
	if (!returnValue) {
		showOWarningDialog(title, alertMessage, okButtonLabel) ;
		if (!(obj.type == 'hidden')) {
			obj.value ="";
			obj.focus();
		}
	}
	return returnValue;
}


//------------------------------------------------------------------------
//
//	Function : isText
//
//	Synopsis : This function is use for check data in object that send to this function 
//				 is(not) text(no number). If data is not text, it will send alert message to user. 
//
//	Arguments : This function receive 2 arguments (2 required) 
//			*	obj - Object that will check value is text. 
//			*	fieldName - String that is name of object and it will  use for alert message. 
//
//	Returns : Boolean that (not)accept data that user key in
//
//	Notes : none
//
//------------------------------------------------------------------------
function isText (obj, fieldName) {
// Set default value for run function
	defaultTitle = "Data not valid"; // Default title of alert dialog
	defaultButtonLabel = "OK"; // Default title of alert dialog
	var returnValue = true; // Variable for return value

// Check value of arguments and assign appropriate value
	alertMessage = "Data in field <B>"+fieldName+"</B> is not text. Please edit data.";

// Check user key any charactor and check value is integer
	if (obj.value != "") {
		var numberString = "0123456789";
		checkString = obj.value;
		for (i=0; i<checkString.length; i++) {
			if (numberString.indexOf(checkString.charAt(i)) != -1) {
				returnValue=false;
				break;
			}
		}
	}

// Show alert if check value is not text
	if (!returnValue) {
		showOWarningDialog(defaultTitle, alertMessage, defaultButtonLabel) ;
		if (!(obj.type == 'hidden')) {
			obj.focus();
		}
	}
	return returnValue;	
}


//------------------------------------------------------------------------
//
//	Function : isInteger
//
//	Synopsis : This function is use for check data in object that send to this function 
//				 is(not) integer. If data is not integer, it will send alert message to user. 
//
//	Arguments : This function receive 2 arguments (2 required) 
//			*	obj - Object that will check value is integer. 
//			*	fieldName - String that is name of object and it will  use for alert message. 
//
//	Returns : Boolean that (not)accept data that user key in
//
//	Notes : none
//
//------------------------------------------------------------------------
function isInteger (obj, fieldName, haveComma, hiddenObj) {
// Set default value for run function
	defaultTitle = "Data not valid"; // Default title of alert dialog
	defaultButtonLabel = "OK"; // Default title of alert dialog
	defaultHaveComma = "no"; // Default comma in float value
	var returnValue = true; // Variable for return value
	intValue = ""; // Variable for keep float value in form of string

// Check value of arguments and assign appropriate value
	haveComma = (haveComma == undefined || haveComma == "") ? defaultHaveComma : haveComma ;
	hiddenObj = (hiddenObj == undefined || hiddenObj == "") ? obj : hiddenObj ;
	alertMessage = "Data in field <B>"+fieldName+"</B> is not integer. Please edit data.";

// Check user key any charactor and check value is integer
	if (obj.value != "") {
		intArray = obj.value.split(",");
		intValue = "";
		for (i=0; i<intArray.length; i++) {
			intValue += intArray[i];
		}
		if (intValue != parseInt(parseFloat(intValue))) {
			returnValue=false;
		}
	}

// Format data for show in text box
	if (returnValue && obj.value != "") {
		intArray = intValue.split(".");
// Find integer with comma
		commaValue = intArray[0];
		if (haveComma.toLowerCase() == "yes") {
			commaValue = "";
			for (i=intArray[0].length-1; i>=0; i--){
				commaValue = intArray[0].charAt(i)+commaValue;
				if (i%3 == intArray[0].length%3 && i != 0) {
					commaValue = ","+commaValue;
				}
			}
		}
// Show integer value
		obj.value = commaValue;
	}

// Show alert if check value is not integer
	if (!returnValue) {
		showOWarningDialog(defaultTitle, alertMessage, defaultButtonLabel) ;
		if (!(obj.type == 'hidden')) {
			obj.focus();
		}
	} else {
		if (haveComma.toLowerCase() == "yes") {
			hiddenObj.value = intValue;
		}
	}
	return returnValue;	
}


//------------------------------------------------------------------------
//
//	Function : isFloat
//
//	Synopsis : This function is use for check data in object that send to this function 
//				 is(not) float. If data is not float, it will send alert message to user. 
//
//	Arguments : This function receive 5 arguments (2 required) 
//			*	obj - Object that will check value is float. 
//			*	fieldName - String that is name of object and it will  use for alert message. 
//				minFraction - Number that sets the minimum fraction digits of float value. 
//				maxFraction - Number that sets the maximum fraction digits of float value. 
//				haveComma - Boolean that (not)sets comma in float value. 
//
//	Returns : Boolean that (not)accept data that user key in
//
//	Notes : none
//
//------------------------------------------------------------------------
function isFloat (obj, fieldName, minFraction, maxFraction, haveComma, hiddenObj, round) {
// Set default value for run function
	defaultTitle = "Data not valid"; // Default title of alert dialog
	defaultButtonLabel = "OK"; // Default title of alert dialog
	defaultMinFraction = 2; // Default fraction digits of float value
	defaultMaxFraction = 2; // Default fraction digits of float value
	defaultHaveComma = "no"; // Default comma in float value
	defaultRound = "normal"; // Default comma in float value
	var returnValue = true; // Variable for return value
	floatValue = ""; // Variable for keep float value in form of string
	numFraction = ""; // Variable for keep number of fraction digits of float value

// Check value of arguments and assign appropriate value
	minFraction = (minFraction == undefined || minFraction < 0) ? defaultMinFraction : minFraction ;
	maxFraction = (maxFraction == undefined || maxFraction < 0) ? defaultMaxFraction : maxFraction ;
	haveComma = (haveComma == undefined || haveComma == "") ? defaultHaveComma : haveComma ;
	round = (round == undefined || round == "") ? defaultRound : round ;
	hiddenObj = (hiddenObj == undefined || hiddenObj == "") ? obj : hiddenObj ;
	alertMessage = "Data in field <B>"+fieldName+"</B> is not real number. Please edit data.";

// Check user key any charactor and check value is float
	if (obj.value != "") {
		floatValue = ""+toFloat(obj.value);
		if (floatValue == "false") {
			returnValue=false;
		}
	}

// Format data for show in text box
	if (returnValue && obj.value != "") {
// Show float value
		obj.value = formatFloat(floatValue, minFraction, maxFraction, haveComma, round);
	}

// Show alert if check value is not float
	if (!returnValue) {
		showOWarningDialog(defaultTitle, alertMessage, defaultButtonLabel) ;
		if (!(obj.type == 'hidden')) {
			obj.focus();
		}
	} else {
		if (haveComma.toLowerCase() == "yes") {
			hiddenObj.value = floatValue;
		}
	}
	return returnValue;	
}


//------------------------------------------------------------------------
//
//	Function : isDate
//
//	Synopsis : This function is use for check data in object that send to this function 
//				 is(not) date. If data is not date, it will send alert message to user. 
//
//	Arguments : This function receive 1 arguments (1 required) 
//			*	obj - Object that will check value is date. 
//
//	Returns : Boolean that (not)accept data that user key in
//
//	Notes : This function want some function in file dateCalendar.js
//
//------------------------------------------------------------------------
function isDate (obj) {
// Call function validateDate for check date value
//	var dateArray = obj.value.split("/");
	return validateDate(obj);	
}

//------------------------------------------------------------------------
//
//	Function : isEmail
//
//	Synopsis : This function is use for check data in object that send to this function 
//				 is(not) e-mail. If data is not an e-mail, it will send alert message to user. 
//
//	Arguments : This function receive 1 arguments (1 required) 
//			*	obj - Object that will check value is in form of e-mail. 
//
//	Returns : Boolean that (not)accept data that user key in
//
//	Notes : none
//
//------------------------------------------------------------------------
function isEmail (obj) {
// Set default value for run function
	defaultTitle = "Data not valid"; // Default title of alert dialog
	defaultAlertMessage = "This is not form of e-mail"; // Default message of alert dialog
	defaultButtonLabel = "OK"; // Default title of alert dialog
	var returnValue = true; // Variable for return value

// Check user key any charactor and check value is e-mail
	if (obj.value != "") {
		adds = obj.value.split("@");
		if (adds.length==2 && adds[0]!="") {
			dot = adds[1].split(".");
			if (dot[0] != "" && dot[dot.length-1] != "") {
				returnValue=true;
			} else  returnValue=false;
		} else returnValue=false;
	}

// Show alert if check value is not an e-mail
	if (!returnValue) {
		showOWarningDialog(defaultTitle, defaultAlertMessage, defaultButtonLabel) ;
		if (!(obj.type == 'hidden')) {
			obj.focus();
		}
	}

	return returnValue;	
}


//------------------------------------------------------------------------
//
//	Function : compareValue
//
//	Synopsis : This function is use for compare data in object that send to this function 
//				It can compare string, number and date. If result of comparison is not correct,
//				it will send alert message to user. 
//
//	Arguments : This function receive 7 arguments (4 required) 
//			*	dataType - String that specifies the data type of comparison. 
//			*	obj1 - Object that have the first value to compare. 
//			*	obj2 - Object that have the second value to compare. 
//				compare - String that sets the type of comparison. 
//				objName1 - String that specifies the name of the first object. 
//				objName2 - String that specifies the name of the second object. 
//				focusField - Number of field that will set focus. 
//
//	Returns : Boolean that is the result of comparison
//
//	Notes : none
//
//------------------------------------------------------------------------
function compareValue(dataType, obj1, obj2, compare, objName1, objName2, focusField, focusObject) {
// Set default value for run function
	title = "Please check data value"; // Default title of alert dialog
	alertMessage = ""; // Default message of alert dialog
	defaultButtonLabel = "OK"; // Default title of alert dialog
	var returnValue = false; // Variable for return value

// Check value of arguments and assign appropriate value
	objName1 = ((objName1 == undefined || objName1 == "") ? obj1.value  : objName1);
	objName2 = ((objName2 == undefined || objName2 == "") ? obj2.value  : objName2);
	focusField = (focusField == undefined) ? 1  : ((focusField == "") ? 0 : focusField);
	focusObject = ((focusObject == undefined || focusObject == "") ? obj1  : focusObject);
	activeObj = (focusField == 1) ? obj1 : obj2 ;
	compareObj = (focusField == 1) ? obj2 : obj1 ;
	activeObjName = (focusField == 1) ? objName1 : objName2 ;
//	compareObjName = (focusField == 1) ? objName2 : objName1 ;

	var checkValue1;
	var checkValue2;
	var canCompare = false;

	if (dataType.toLowerCase() == "string") {
		title = "Please check string value";
		checkValue1 = obj1.value;
		checkValue2 = obj2.value;
		if (checkValue1 != "" && checkValue2 != "")
			canCompare = true;
	} else if (dataType.toLowerCase() == "integer") {
		if (isInteger(activeObj, activeObjName) && activeObj.value != "" && compareObj.value != "") {
			canCompare = true;
			title = "Please check number value";
			checkValue1 = parseInt(parseFloat(obj1.value));
			checkValue2 = parseInt(parseFloat(obj2.value));
		}
	} else if (dataType.toLowerCase() == "float") {
		if (activeObj.value != "" && compareObj.value != "") {
			canCompare = true;
			title = "Please check number value";
			checkValue1 = parseFloat(obj1.value);
			checkValue2 = parseFloat(obj2.value);
		}
	} else if (dataType.toLowerCase() == "date") {
		if (isDate(activeObj) && activeObj.value != "" && compareObj.value != "") {
			canCompare = true;
			title = "Please check date value";
			date1= obj1.value.split("/");
			date2= obj2.value.split("/");
			checkValue1 = date1[2] + date1[1] + date1[0];
			checkValue2 = date2[2] + date2[1] + date2[0];
		}
	}

if (canCompare) {
	if (compare == "=") {
		alertMessage = objName1 + " must equal " + objName2;
		if (checkValue1 == checkValue2) {
			returnValue = true;
		}
	}
	if (compare == "!=") {
		alertMessage = objName1 + " must not equal " + objName2;
		if (checkValue1 != checkValue2) {
			returnValue = true;
		}
	}

	if (compare == ">=") {
		alertMessage = objName1 + " must more than or equal " + objName2;
		if (checkValue1 >= checkValue2) {
			returnValue = true;
		}
	}

	if (compare == ">") {
		alertMessage = objName1 + " must more than " + objName2;
		if (checkValue1 > checkValue2) {
			returnValue = true;
		}
	}

	if (compare == "<") {
		alertMessage = objName1 + " must less than " + objName2;
		if (checkValue1 < checkValue2) {
			returnValue = true;
		}
	}

	if (compare == "<=") {
		alertMessage = objName1 + " must less than or equal " + objName2;
		if (checkValue1 <= checkValue2) {
			returnValue = true;
		}
	}
} else {
	returnValue = true;
}

	if (!returnValue) {
		showOWarningDialog(title, alertMessage, defaultButtonLabel);
		activeObj.value = "";//compareObj.value;
		focusObject.focus();
/*
		if (focusField == 1) {
			obj1.focus();
		} else if (focusField == 2) {
			obj2.focus();
		}
*/
	}

	return returnValue;
}


function isCommonKey() {
	var returnValue = false;
// Common Key are Tab, Caps Lock, Shift, Ctrl, Alt, Backspace, Insert, Delete, Home, End, Arrow left (<--), Arrow right (-->)
//alert(event.keyCode);
	if (event.keyCode== 9 || event.keyCode== 20 || event.keyCode== 16 || event.ctrlKey || event.altKey || event.keyCode==8 || event.keyCode==45 || event.keyCode==46 || event.keyCode==36 || event.keyCode==35 || event.keyCode==37 || event.keyCode==39) {
		returnValue =  true;
	}
	return returnValue;
}


function isKeyText() {
	var returnValue = false;
	returnValue = isCommonKey();
	if (!returnValue) {
// Can key-in every text except 0-9
		if (!(event.keyCode<=105 && event.keyCode>=96)) {
			returnValue =  true;
		}
	}
	return returnValue;
}


function isKeyInteger() {
	var returnValue = false;
	returnValue = isCommonKey();
	if (!returnValue) {
// Only "-", "," and 0-9 Key-in Textfield
		if ((event.keyCode<=57 && event.keyCode>=48) || (event.keyCode<=105 && event.keyCode>=96) || event.keyCode== 109 || event.keyCode== 189 || event.keyCode== 188) {
			returnValue =  true;
		}
	}
	return returnValue;
}


function isKeyFloat() {
	var returnValue = false;
	returnValue = isCommonKey();
	if (!returnValue) {
// Only "-", ".", "," and 0-9 Key-in Textfield
		if ((event.keyCode<=57 && event.keyCode>=48) || (event.keyCode<=105 && event.keyCode>=96) || (event.keyCode<=110 && event.keyCode>=109) || (event.keyCode<=190 && event.keyCode>=188)) {
			returnValue =  true;
		}
	}
	return returnValue;
}


function isKeyDate() {
	var returnValue = false;
	returnValue = isCommonKey();
	if (!returnValue) {
// Only "-", ".", "/" and 0-9 Key-in Textfield
		if ((event.keyCode<=57 && event.keyCode>=48) || (event.keyCode<=105 && event.keyCode>=96) || (event.keyCode<=111 && event.keyCode>=109) || (event.keyCode<=191 && event.keyCode>=189)) {
			returnValue =  true;
		}
	}
	return returnValue;
}

// check length for textArea component when fix maxLength
function checkLength(obj,length)
{	
	if (obj.value.length < length)
		return true;
	else
	{
		obj.value = obj.value.substring(0,length);
		return false;
	}
}

//
//	Function : isEquals
//
//	Returns : Boolean that (not)accept data that object1 equals with object2
//
//	Notes : none
//
//------------------------------------------------------------------------
function isEquals (obj1,obj2, fieldName1,fieldName2) {
// Set default value for run function
	defaultTitle = "Data not valid"; // Default title of alert dialog
	defaultButtonLabel = "OK"; // Default title of alert dialog
	var returnValue = true; // Variable for return value
	var password = "Object 1";
	var repassword = "Object 2";
	if( fieldName1!= undefined)
		password = fieldName1
	if( fieldName2!= undefined)
		repassword=fieldName2

// Check value of arguments and assign appropriate value
	alertMessage = "Data in field <B>"+password+"</B> is not equals with <B> "+repassword+" <B>. Please edit data.";

// Check user key any charactor and check value is integer
	if (obj1.value != obj2.value) {
				returnValue=false;
	}

// Show alert if check value is not text
	if (!returnValue) {
		showOWarningDialog(defaultTitle, alertMessage, defaultButtonLabel) ;
		if (!(obj1.type == 'hidden')) {
			obj1.focus();
		}
	}
	return returnValue;	
}
		